﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YT.DB;

 
namespace YT.Commons
{
    public class CommonClass
    {
    }
    #region Convert Class
    public class CvtFunc
    {
        public static string ToString(object obj)
        {
            if (obj == null) return "";

            return obj.ToString().TrimEnd();
        }


        public static string ToLeftString(object obj, int iLength)
        {
            if (obj == null) return "";
            string strTo = obj.ToString().TrimEnd();
            if (strTo.Length > iLength)
                return strTo.Substring(0, iLength);
            else
                return strTo;
        }
        public static string ToRightString(object obj, int iLength)
        {
            if (obj == null) return "";
            string strTo = obj.ToString().TrimEnd();
            if (strTo.Length > iLength)
                return strTo.Substring(strTo.Length - iLength);
            else
                return strTo;
        }
        public static string ToSubstring(object obj, int iStartIndex, int iLength)
        {
            if (obj == null) return "";
            string strTo = obj.ToString().TrimEnd();
            if (iStartIndex > strTo.Length || iStartIndex < 0 || iLength < 0)
            {
                return "";
            }
            else if (iStartIndex + iLength > strTo.Length)
            {
                return strTo.Substring(iStartIndex); ;
            }
            else
                return strTo.Substring(iStartIndex, iLength);
        }


        public static string ToDateString(object obj)
        {
            if (obj == null || obj.ToString().TrimEnd() == "") return "";

            return BaseFunctions.FormatDate(obj.ToString());
        }

        public static DateTime ToDateTime(object obj)
        {
            if (obj == null || obj.ToString().TrimEnd() == "" || obj.ToString().Trim() == "/  /") return new DateTime(1900, 1, 1);

            return DateTime.Parse(obj.ToString());
        }

        public static double ToDouble(object obj)
        {
            return ToDouble(obj, -1);
        }
        public static double ToDouble(object obj, int intDecimalPoint)
        {
            string strFormatStyle = "";

            if (obj == null || obj.ToString().TrimEnd() == "") return 0;

            if (intDecimalPoint >= 0)
            {
                strFormatStyle = "N." + intDecimalPoint.ToString();
                return double.Parse(BaseFunctions.FormatNumeric(obj.ToString(), strFormatStyle));
            }
            else
            {
                //return double.Parse(obj.ToString());

                return double.Parse(BaseFunctions.FormatNumeric(obj, "n.n"));

            }
        }

        public static float ToFloat(object obj) //Int64
        {
            if (obj == null || obj.ToString().TrimEnd() == "") return 0;

            return float.Parse(obj.ToString());
        }

        public static int ToInt(object obj)  //Int32
        {
            if (obj == null || obj.ToString().TrimEnd() == "" || !BaseFunctions.IsNumeric(obj.ToString())) return 0;

            return int.Parse(BaseFunctions.FormatNumeric(obj, "n.0"));
        }

        public static long ToLong(object obj) //Int64
        {
            if (obj == null || obj.ToString().TrimEnd() == "") return 0;

            return long.Parse(obj.ToString());
        }

        public static bool ToBoolean(object obj)
        {
            if (obj == null || obj.ToString().TrimEnd() == "") return false;

            if (obj.ToString().ToUpper() == "YES") return true;
            if (obj.ToString().ToUpper() == "NO") return false;

            return Convert.ToBoolean(obj.ToString());
        }


    }
    #endregion Convert Class

    #region SystemDateOption class
    public class SystemDateOption
    {
        public static string optDateFmtMDY
        {
            get { return "MM/dd/yyyy"; }
        }
        public static string optFmtDtp
        {
            get { return "MM/dd/yyy"; }
        }
        public static string optDateFmtYMD
        {
            get { return "yyyy/MM/dd"; }
        }
        public static string optDateFmtMY
        {
            get { return "MM/yyyy"; }
        }
        public static string GetFormat(DateFormatOption opt)
        {
            switch (opt)
            {
                case DateFormatOption.DateFmtMDY:
                    return SystemDateOption.optDateFmtMDY;
                case DateFormatOption.FmtDtp:
                    return SystemDateOption.optFmtDtp;
                case DateFormatOption.DateFmtYMD:
                    return SystemDateOption.optDateFmtYMD;
                case DateFormatOption.DateFmtMY:
                    return SystemDateOption.optDateFmtMY;

            }
            return "";

        }
    }
    #endregion
  
}
